//
//  PolygonObject.h
//  Box2DProject
//
//  Created by Thi Huynh on 9/12/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "cocos2d.h"
#import "PRTriangulator.h"
#import "GB2ShapeCache.h"
#import "Const.h"
#import "MathHelper.h"
#import "ResourceDef.h"

@interface PolygonObject : CCSprite
{
    
    b2Body          *_body;
    int             _areaTrianglePointCount;
    int             _vertexCount;
    ccBlendFunc blendFunc;
	
	CGPoint *areaTrianglePoints;
	CGPoint *textureCoordinates;
    
    id<PRTriangulator> _triangulator;
    
    NSString    *_fileName;
    BOOL        _isDestroyed;
    BOOL        _isStartSlice;
    BOOL        _isEndSlice;
    float       _sliceTime;
    
    b2Vec2      _centroid;
    b2Vec2      _startSlicePoint;
    b2Vec2      _endSlicePoint;
    b2Vec2      *_shapeVertices;
    
}

@property (nonatomic, retain) id<PRTriangulator> triangulator;
@property (nonatomic, readonly) b2Body      *body;
@property (nonatomic, readonly) b2Vec2      *shapeVertices;
@property (nonatomic, retain)   NSString    *fileName;
@property (nonatomic)           BOOL        isDestroyed;
@property (nonatomic)           BOOL        isStartSlice;
@property (nonatomic)           BOOL        isEndSlice;
@property (nonatomic)           float       sliceTime;
@property (nonatomic)           b2Vec2      centroid;
@property (nonatomic)           b2Vec2      startSlicePoint;
@property (nonatomic)           b2Vec2      endSlicePoint;
/**
 Returns an sprite polygon with body and additional properties.
 */
- (id)initWithFileName:(NSString*)name body:(b2Body*)body vertices:(b2Vec2*)vertices verticeCnt:(int)verticeCnt destroyed:(BOOL)destroyed;

/**
 Returns an sprite polygon with body and additional properties.
 */
- (id)initWithFileName:(NSString*)name body:(b2Body*)body destroyed:(BOOL)destroyed;
/**
 Returns an sprite polygon with specified position with name is image file.
 */
- (id)initPolygonWith:(b2World*)world fileName:(NSString*)name ;

/**
 Returns an sprite polygon with name is image file.
 */
- (id)initPolygonWith:(b2World*)world fileName:(NSString*)name andPos:(CGPoint)pos;

/**
 Returns an autoreleased polygon.  Default triangulator is used (Ratcliff's).
 */
+ (id) filledPolygonWithPoints: (NSArray *) polygonPoints andFileName:(NSString *)name;

/**
 Returns an autoreleased filled poly with a supplied triangulator.
 */
+ (id) filledPolygonWithPoints:(NSArray *)polygonPoints andFileName:(NSString *)name usingTriangulator: (id<PRTriangulator>) polygonTriangulator;

/**
 Initialize the polygon.  polygonPoints will be triangulated.  Default triangulator is used (Ratcliff).
 */
- (id) initWithPoints: (NSArray *) polygonPoints andFileName:(NSString *)name;

/**
 Initialize the polygon.  polygonPoints will be triangulated using the supplied triangulator.
 */
- (id) initWithPoints:(NSArray *)polygonPoints andFileName:(NSString *)name usingTriangulator: (id<PRTriangulator>) polygonTriangulator;

/**
 active collision.
 */
- (void)activateCollisions;
/**
 deactive collision.
 */
- (void)deactivateCollisions;

/**
 create body with additional param
 @Param world: physic world
 @param position: body position first time
 @param rotation: body rotation first time
 @param vertices: array of polygon vertice
 @param count:  number of vertice
 @param density : mass of body
 @param friction : body friction index
 @param restitution : body restitution index
 
 @return : b2body
 */
- (b2Body*)createBodyWith:(b2World*)world 
                    position:(b2Vec2)position 
                    rotation:(float)rotation 
                    vertices:(b2Vec2*)vertices 
                 vertexCount:(int32)count 
                     density:(float)density 
                    friction:(float)friction 
                 restitution:(float)restitution;

@end
